﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;

//using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

using Baosight.ZNH.ZMCA.UI.Controls;
using Baosight.ZNH.ZMCA.UI.Utils;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Gauges;
using Telerik.Windows.Controls.Charting;
using Baosight.ZNH.ZMCA.UI.Model;

namespace Baosight.ZNH.ZMCA.UI
{
    public partial class ZMCA06 : EFView
    {
        public ZMCA06Controler controler = new ZMCA06Controler();

        public ZMCA06()
        {
            InitializeComponent();

            InitGaugeData(this.indexGauge);
           

        }

        #region 填充控件数据

        private void InitGaugeData(DoubleCircleGauge gauge)
        {
            controler.GetGaugeData(gauge);
        }
        #endregion

    }



    public class ZMCA06Controler
    {
        private DoubleCircleGauge gauge;
      
        private int empCount = 0;   //记录全矿当前人数

        public ZMCA06Controler()
        {

        }

        #region 查询加载Gauge图

        /// <summary>
        /// 初始化指标图，查询数据
        /// </summary>
        public void GetGaugeData(DoubleCircleGauge gauge)
        {
            this.gauge = gauge;
           
            this.gauge.gaugeTitle.Text = "工效";
            this.gauge.leftGauge.Tag = "ZMCA02";
            this.gauge.rightGauge.Tag = "ZMCA01";

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA01");
            eiInfo.Set("method", "queryByIndex");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, QueryGaugeDataDoWorkCompleted);
        }

        /// <summary>
        /// 查询指标图数据回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryGaugeDataDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eiInfo = e.EIInfo;
            if (e.EIInfo.Status == -1)
            {
                Common.Alert(Common.MsgLoadDataError);
                return;
            }

            EIBlock result = eiInfo.GetBlock("result");

            int empCount = int.Parse(eiInfo.Get("empCount").ToString());
            int monthPerson = int.Parse(eiInfo.Get("MonthPerson").ToString());
            int currentMonth = int.Parse(eiInfo.Get("month").ToString());
            int currentYear = int.Parse(eiInfo.Get("year").ToString());
            int dayOfMonth = DateTime.DaysInMonth(currentYear, currentMonth);

            double AllEffPlan = 0;
            double AllEffActual = 0;
            double WorkEffPlan = 0;
            double WorkEffActual = 0;
            double monthPlan = 0;
            double monthActual = 0;

            if (result != null && result.Rows.Count > 0)
            {
                foreach (var item in result.Rows)
                {
                    int dataType = Common.GetIntValue(item["fdPlanType"]);
                    //通过dataType判断数据类型,0年计划， 1月计划，20年实际， 30月实际， 35昨日计划， 40昨日
                    if (dataType == 1)
                    {
                        monthPlan = Common.GetDoubleValue(item["fdPlanMine"]);
                    }
                    else if (dataType == 30)
                    {
                        monthActual = Common.GetDoubleValue(item["fdPlanMine"]);
                    }
                }
            }

            //计算全员月工作效率（计划和实际）
            if (monthPerson != 0)
            {
                AllEffPlan = monthPlan /  (dayOfMonth * empCount);
                AllEffActual = monthActual /  (dayOfMonth * empCount);
            }
            //计算单位，并将指标值缩小到100以内
            int monthUnit = ZMCA03Controler.GetUnit(AllEffActual);
            //全员月工作效率（计划和实际）
            AllEffPlan = AllEffPlan / monthUnit;
            AllEffActual = AllEffActual / monthUnit;
            string unit = "吨";
            gauge.leftGaugeDescription.Text = monthUnit >= 10 ? "全员：" + Common.GetChineseUnit(monthUnit) + unit + "/人天" : "全员：" + unit + "/人天";
            gauge.leftNeedle.Value = AllEffActual;

            //计算回采面月工作效率（计划和实际）
            if (monthPerson != 0)
            {
                WorkEffPlan = monthPlan / monthPerson;
                WorkEffActual = monthActual / monthPerson;
            }
            monthUnit = ZMCA03Controler.GetUnit(WorkEffActual);
            //回采面月工作效率（计划和实际）
            WorkEffPlan = WorkEffPlan / monthUnit;
            WorkEffActual = WorkEffActual / monthUnit;

           
            gauge.rightGaugeDescription.Text = monthUnit >= 10 ? "回采：" + Common.GetChineseUnit(monthUnit) + unit + "/人天" : "回采：" + unit + "/人天";          
            gauge.rightNeedle.Value = WorkEffActual;

        }

        #endregion




        
    }
}
